(function () {
  const elevator = document.querySelector('.xtx-elevator')
  const backTop=document.querySelector('#backTop')
  window.addEventListener('scroll', function () {
    const index = this.document.documentElement.scrollTop
    if (index >= 300) {
      elevator.style.opacity=1
    } else {
      elevator.style.opacity=0
    }
  })
  backTop.addEventListener('click', function () {
    window.scrollTo(0,0)
  })
})();

(function () {
  const list = document.querySelector('.xtx-elevator-list')
  const category=document.querySelector('.xtx_goods_category')
  const goodNews = document.querySelector('.xtx_goods_new')
  const popular = document.querySelector('.xtx_goods_popular')
  const brand = document.querySelector('.xtx_goods_brand')
  const topic = document.querySelector('.xtx_goods_topic')
  
  list.addEventListener('click', function (e) {
    if (e.target.tagName === 'A'&& e.target.dataset.name) {
      const uname = e.target.dataset.name
      switch (uname) {
        case 'goodNews':
          window.scrollTo(0, goodNews.offsetTop)
          break
          case 'popular':
            window.scrollTo(0, popular.offsetTop)
          break
          case 'brand':
            window.scrollTo(0, brand.offsetTop)
          break
          case 'topic':
            window.scrollTo(0, topic.offsetTop)
          break
        default:
          alert('我真帅')
      }
    }
  })

  window.addEventListener('scroll', function () {
    const h = document.documentElement.scrollTop
    list.querySelector('.active') && list.querySelector('.active').classList.remove('active')
    if (h>=goodNews.offsetTop&&h<popular.offsetTop) {
      list.querySelector('a[data-name="goodNews"]').classList.add('active')
    } else if (h>= popular.offsetTop&&h<brand.offsetTop) {
      list.querySelector('a[data-name="popular"]').classList.add('active')
    } else if (h>= brand.offsetTop&&h<category.offsetTop) {
      list.querySelector('a[data-name="brand"]').classList.add('active')
    } else if (h>= topic.offsetTop) {
      list.querySelector('a[data-name="topic"]').classList.add('active')
    }
  })
})();